#include<bits/stdc++.h>
using namespace std;
int n,m;
int fakea[100005],a[100005],c[100005],x[5],ans[100005],f[100005];
char d[25][100005];
vector<int> v;
int race(int round,int n){
    int cnt=0;
    for(int i=1;i<=n;i++){
        if(!f[i]) v.push_back(i);
        if(v.size()==2){
            int p1=v[0],p2=v[1];
            cnt++;
            if(d[round][cnt]=='0'){
                if(a[p1]<round) f[p1]=1;
            }
            if(d[round][cnt]=='1'){
                if(a[p2]<round) f[p2]=1;
            }
            v.clear();
        }
    }
    if(!(v.size()-1)){
        for(int i=1;i<=n;i++){
            if(!f[i]) return i;
        }
    }
    return race(round+1,n);
}
int main(){
    freopen("arena.in","r",stdin);
    freopen("arena.out","w",stdout);
    cin>>n>>m;
    for(int i=1;i<=n;i++) cin>>fakea[i];
    for(int i=1;i<=m;i++) cin>>c[i];
    int k=ceil(log2(n));
    for(int i=1;i<=k;i++){
        for(int j=1;j<=pow(2,k-1);j++){
            cin>>d[i][j];
        }
    }
    int T;cin>>T;
    while(T--){
        int ans=0;
        for(int i=0;i<4;i++) cin>>x[i];
        for(int i=1;i<=n;i++){
            a[i]=fakea[i]^x[i%4];
        }
        for(int i=1;i<=n;i++){
            memset(f,0,sizeof(f));
            int winner=race(1,c[i]);
            ans^=i*winner;
        }
        cout<<ans<<endl;
    }
    return 0;
}
